'use strict';

const Controller = require('egg').Controller;

class PropertiesController extends Controller {
  async list() {
    const { ctx } = this;
    try {
      const data = await ctx.service.property.list(ctx.query || {});
      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取房源列表失败' };
    }
  }

  async detail() {
    const { ctx } = this;
    const { id } = ctx.params;
    if (!id || isNaN(id)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '参数错误' };
      return;
    }
    try {
      const data = await ctx.service.property.detail(id);
      if (!data) {
        ctx.status = 404;
        ctx.body = { success: false, message: '房源不存在' };
        return;
      }
      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取房源详情失败' };
    }
  }

  // 创建房源
  async create() {
    const { ctx } = this;
    const user_id = ctx.state.user?.id; // 从认证中间件获取

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    try {
      const data = { ...ctx.request.body, user_id };
      const result = await ctx.service.property.create(data);
      
      ctx.status = 201;
      ctx.body = { 
        success: true, 
        message: '房源发布成功', 
        data: { id: result.insertId } 
      };
    } catch (e) {
      ctx.status = 400;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 更新房源
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    if (!id || isNaN(id)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '参数错误' };
      return;
    }

    try {
      // 检查房源是否属于当前用户
      const property = await ctx.service.property.detail(id);
      if (!property || property.user_id !== user_id) {
        ctx.status = 403;
        ctx.body = { success: false, message: '无权限操作' };
        return;
      }

      await ctx.service.property.update(id, ctx.request.body);
      ctx.body = { success: true, message: '房源更新成功' };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 删除房源
  async delete() {
    const { ctx } = this;
    const { id } = ctx.params;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    if (!id || isNaN(id)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '参数错误' };
      return;
    }

    try {
      // 检查房源是否属于当前用户
      const property = await ctx.service.property.detail(id);
      if (!property || property.user_id !== user_id) {
        ctx.status = 403;
        ctx.body = { success: false, message: '无权限操作' };
        return;
      }

      await ctx.service.property.delete(id);
      ctx.body = { success: true, message: '房源删除成功' };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 获取用户的房源列表
  async getUserProperties() {
    const { ctx } = this;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    try {
      const data = await ctx.service.property.getUserProperties(user_id, ctx.query);
      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取房源列表失败' };
    }
  }
}

module.exports = PropertiesController;